// pages/detail/detail.js
var app = getApp()

var WxParse = require('../../wxParse/wxParse.js')
import {
  getProductDetail
} from '../../config/index.js'

Page({

  /**
   * 页面的初始数据
   */

  data: {
    isGoIndex: true,
    iShidden: true,
    info: {
      product: {
        id: '',
        price: '',
        name: '',
        main_img: '',
        time: '',
        unit: '',
        service_category_id: '',
        content: ''
      },
      address: {
        id: '',
        name: '',
        phone: '',
        address: '',
        province_id: '',
        city_id: '',
        district_id: ''
      },
      time: {
        start_at: '',
        end_at: '',
        hours: 1
      },
      cleaner: {
        id: '',
        wx_avatar_url: '',
        real_name: ''
      },
      coupon: {
        id: '',
        price: '',
      },
      remark: '',
      totalPrice: 0,
      textarea: true
    },
    selUnit: 0,
    maskShow: false,
    skuNum: 0,
    skuList: [{
        id: "1",
        value: "1"
      },
      {
        id: "2",
        value: "2"
      },
      {
        id: "3",
        value: "3"
      },
      {
        id: "4",
        value: "4"
      },
      {
        id: "5",
        value: "5"
      },
      {
        id: "6",
        value: "6"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      info: JSON.parse(options.info)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initApp()
    this.setData({
      'info.time.hours': this.data.skuList[0].value
    })
  },

  /**
   * initApp
   */
  initApp: function() {
    let _this = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    getProductDetail(_this.data.info.product.id).then(res => {
      if (res.status === 1) {
        WxParse.wxParse('content', 'html', res.data.detail.content, _this, 5);
        _this.setData({
          'info.product': res.data.detail
        })
        wx.hideLoading()
      }
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
    })
  },

  confirmOrder: function() {
    let that = this
    delete that.data.info.product.content
    wx.redirectTo({
      url: '/pages/confirm/confirm?info=' + JSON.stringify(that.data.info),
    })
  },

  closeMask: function() {
    this.setData({
      maskShow: false
    })
  },

  showSku: function() {
    this.setData({
      maskShow: true
    })
  },

  selectSku: function(e) {
    this.setData({
      'info.time.hours': e.currentTarget.dataset.sku
    })
  }
})